package com.ruoyi.web.controller.projectAch;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.projectAch.domain.SchoolName;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.projectAch.domain.GrantingUnit;
import com.ruoyi.projectAch.service.IGrantingUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 维护授予单位Controller
 * 
 * @author ruoyi
 * @date 2023-11-12
 */
@RestController
@RequestMapping("/projectAch/grantingUnit")
public class GrantingUnitController extends BaseController
{
    @Autowired
    private IGrantingUnitService grantingUnitService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询维护授予单位列表
     */
    @PreAuthorize("@ss.hasPermi('projectAch:grantingUnit:list')")
    @GetMapping("/list")
    public TableDataInfo list(GrantingUnit grantingUnit)
    {
        startPage();
        List<GrantingUnit> list = grantingUnitService.selectGrantingUnitList(grantingUnit);
        return getDataTable(list);
    }

    /**
     * 导出维护授予单位列表
     */
    @PreAuthorize("@ss.hasPermi('projectAch:grantingUnit:export')")
    @Log(title = "维护授予单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GrantingUnit grantingUnit)
    {
        List<GrantingUnit> list = grantingUnitService.selectGrantingUnitList(grantingUnit);
        ExcelUtil<GrantingUnit> util = new ExcelUtil<GrantingUnit>(GrantingUnit.class);
        util.exportExcel(response, list, "维护授予单位数据");
    }

    /**
     * 获取维护授予单位详细信息
     */
    @PreAuthorize("@ss.hasPermi('projectAch:grantingUnit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(grantingUnitService.selectGrantingUnitById(id));
    }

    /**
     * 新增维护授予单位
     */
    @PreAuthorize("@ss.hasPermi('projectAch:grantingUnit:add')")
    @Log(title = "维护授予单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GrantingUnit grantingUnit)
    {
        return toAjax(grantingUnitService.insertGrantingUnit(grantingUnit));
    }

    /**
     * 修改维护授予单位
     */
    @PreAuthorize("@ss.hasPermi('projectAch:grantingUnit:edit')")
    @Log(title = "维护授予单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GrantingUnit grantingUnit)
    {
        return toAjax(grantingUnitService.updateGrantingUnit(grantingUnit));
    }

    /**
     * 删除维护授予单位
     */
    @PreAuthorize("@ss.hasPermi('projectAch:grantingUnit:remove')")
    @Log(title = "维护授予单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(grantingUnitService.deleteGrantingUnitByIds(ids));
    }

    @Log(title = "导入学校名称", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<GrantingUnit> util = new ExcelUtil<GrantingUnit>(GrantingUnit.class);
        List<GrantingUnit> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = grantingUnitService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        return util.importTemplateExcel("用户数据");
    }
}
